@extends('wap.layout')
@section('content')
	<!--文件信息-->
	<div class="fileInfos">
		<!--上传用户-->
		<div class="upUser">
			<span><img src=""></span>
			<span id="user_name">用户名</span>
		</div>
		<!--上传用户end-->
		<!--文件名称等-->
		<div class="fInfo">
			<div class="fType">
				<i class="fileIcon"></i>
			</div>
			<div class="fInfoBar">
				<div class="fName">
					文件名.doc
				</div>
				<div class="fOpt">
					<a href="javascript:void(0);" class="preview">预览</a>
					<a href="javascript:void(0);" class="download">下载</a>
					<a href="javascript:void(0);" class="fav">收藏</a>
					<a href="javascript:void(0);" class="del">删除</a>
				</div>
			</div>
		</div>
		<!--文件名称等end-->
	</div>
	<div class="weui-cells__title">文件信息</div>
	<div class="weui-cells info">
	  <div class="weui-cell">
	    <div class="weui-cell__bd">
	    	<p>文件大小：<i id="storage_size">345K</i></p>
	    	{{--<p>文件位置：<i id="location">测试</i></p>--}}
	    	<p>更新时间：<i id="updated_at">2017-11-2 14:37:19</i></p>
	    	<p>创建时间：<i id="created_at">2017-11-2 14:37:19</i></p>
	    </div>
	  </div>
	</div>
	<!--文件信息end-->
	{{--<!--回复-->
	<div class="weui-cells__title">回复(0)</div>
	<div class="weui-cells msgBlock">
	  <div class="weui-cell">
	    <div class="weui-cell__bd">
	    	<!--消息块-->
	    	<div class="msgItem">
	    		<div class="uHead"><img src="test/wyPic2.jpg"></div>
	    		<div class="msgBox">
	    			<i>2017-11-3 10:58:19</i>
	    			<div class="msgDetail">
	    				<span>王小二</span>
	    				<span>我是内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容</span>
	    			</div>
	    			<div class="msgDel">
	    				<a href="javascript:void(0);">删除</a>
	    			</div>
	    		</div>
	    	</div>
	    	<!--消息块end-->
	    	<!--消息块-->
	    	<div class="msgItem">
	    		<div class="uHead"><img src="test/wyPic2.jpg"></div>
	    		<div class="msgBox">
	    			<i>刚刚</i>
	    			<div class="msgDetail">
	    				<span>王小二</span>
	    				<span>内容</span>
	    			</div>
	    			<div class="msgDel">
	    				<a href="javascript:void(0);">删除</a>
	    			</div>
	    		</div>
	    	</div>
	    	<!--消息块end-->
	    </div>
	  </div>
	</div>
	<!--回复end-->--}}
	
	<div class="placeHolderBar"></div>
	<!--回复区域-->
	<div class="pubBox" id="shareArea">
		<div class="pubOuterBox">
			<div class="pubOtItem">
				<div class="weui-cells">
			      <div class="weui-cell weui-cell_select-before">
			        <div class="weui-cell__hd">
			          <a href="javascript:void(0);" class="atIcon" id="atIcon"></a>
			        </div>
			        <div class="weui-cell__bd">
			          <textarea class="weui-input editorCont" id="editorCont" placeholder="点名@人可转发本文件" readonly></textarea>
			        </div>
			        <div class="weui-cell__hd last">
			          <a href="javascript:void(0);" class="pubAbtn">转发</a>
			        </div>
			      </div>
			    </div>
			        
			</div>
		</div>
	</div>
	<div class="pubBoxTop">
		<textarea class="editorContTop" id="editorContTop"></textarea>
		<div class="pubBtnBar">
			<a class="pubBtnCancel" href="javascript:void(0);">取消</a>
			<a class="pubBtnReply" href="javascript:void(0);">回复</a>
		</div>
	</div>
	<div class="mask"></div>
	<!--回复区域end-->
<a href="javascript:;" id='show-custom' class="weui-btn weui-btn_primary" style="display:none;">显示自定义对话框</a>
@stop

@section('ownJs')
<script>
	var file_id = getParameterByName('user_file_id');
	var is_public = parseInt(getParameterByName('is_public'));
	var atUserStr = getcookie('atUserStr');
	var atUserId = '',atUserName = '',download_links,file_type='',folder_id;
$(function() {
	getFile();
	getParentId();
	if(is_public == 1) $('.fOpt a.del').remove();
	if (atUserStr != '') {
		atUserList = atUserStr.split(',');
		userId = [],userName = [];
		$.each(atUserList,function(k,v){
			userId.push(v.split('|')[0]);
			userName.push(v.split('|')[1]);
		});
		atUserId = userId.join(',');
		atUserName = userName.join(' @');
		$('#editorCont').val('@'+atUserName);
	}
	$('#editorCont, #atIcon').click(function(){
		location.href = '/userList?user_file_id='+file_id+'&is_public='+is_public;
	});
	$('.pubAbtn').click(function(){
		if(atUserName == ''){
			$.alert("请选择发送人")
			return false;
		}
		$.ajax({
			type: 'get',
			url: ' /user_file/forward/'+file_id,
			data:{atUserId:atUserId,is_public:is_public},
			dataType: 'json',
			success: function (result) {console.log(result);
				if (result.code == 0) {
					$.toast("转发成功！");
					addcookie('atUserStr', '', -1);
					setTimeout(function() {
						location.href = location.href;
					}, 1000)
				}else if(result.code == 1){
					$.toast("文件不存在", "cancel");
					setTimeout(function() {
						self.location=document.referrer;
					}, 1000)
				}else if(result.code == -1){
					location.href = '/error';
				}
			}
		});
	});
	//删除文件弹框
	$(document).on("click", ".del", function() {
        $.confirm("您确定要删除文件吗?", "确认删除?", function() {
			deleteFile();
        }, function() {
          //取消操作
        });
    });
	//删除留言弹框
	$(document).on("click", ".msgDel a", function() {
        $.confirm("删除留言?", "确认删除?", function() {
          $.toast("留言已经删除!");
        }, function() {
          //取消操作
        });
    });
	//文件菜单操作
	$(document).on("click", ".fOpt .fav", function() {
		if($(this).hasClass("ck")){
			cancelCollectFile();
		}else{
			collectFile();
		}
	});
	//下载文件
	$(document).on("click", ".download", function() {
		if(file_type == 'at'){
			var access_token =$(this).attr('access_token');
			var is_image =$(this).attr('is_image');
			if(is_image == '0'){
				var u = navigator.userAgent, app = navigator.appVersion;
				var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
				if(isIOS){
					download_links = "{{ Illuminate\Support\Facades\Request::server ('SERVER_NAME') }}" +'/download/' + access_token;
					$('#show-custom').trigger('click');
				}else{
					location.href = '/download/' + access_token;
				}
			}else{
				location.href = '/download/' + access_token;
			}
		}else {
			download();
		}
	});
	$(document).on("click", "#show-custom", function() {
		$.modal({
			title: "",
			text: download_links,
			buttons: [
				{ text: "长按复制该链接到浏览器中下载", className: "default"},
//				{ text: "确定", onClick: function(){ $.alert("确定"); } },
			]
		});
	});
});
	function getParentId(){
		$.ajax({
			type: 'get',
			url: ' /user_file/parent/'+file_id,
			data:{is_public:is_public},
			dataType: 'json',
			success: function (result) {
				if (result.code == 0) {
					folder_id = result.data.parent_id;
				}else if(result.code == -1){
					location.href = '/error';
				}
			}
		});
	}
	function getFile(){
		$.ajax({
			type: 'get',
			url: ' /user_file/detail/'+file_id,
			data:{is_public:is_public},
			dataType: 'json',
			success: function (result) {console.log(result);
				if (result.code == 0) {
					var detail = result.data.detail;
					$('#user_name').text(detail.user_name);
					$('.upUser img').attr('src', detail.user_avatar);
					$('.fName').text(detail.name+detail.file_ext);
					var is_image = 0;
					if(detail.thumb){
						is_image = 1;
						var img_html = '<img src="'+ detail.thumb +'" alt="" style="width: 100%;height: 100%;" class="weui-media-box__thumb">';
						$('.fType').html(img_html);
						$('.preview').attr('href', detail.thumb);
					}else{
						$('.fType .fileIcon').addClass(getIconClass(detail.file_ext.toUpperCase()));
						$('.preview').hide();
					}
					$('.download').attr('is_image', is_image).attr('access_token', detail.access_token);
					if(detail.type == 2){
						$('.fav').remove();//如果是分享链接无法收藏
						$('#shareArea').remove();//无法转发
						file_type='at';
					}else {
						if (detail.collect_status == 1) {
							$(".fOpt .fav").toggleClass("ck");
						}
					}
					$('#storage_size').text(detail.storage_size);
//					$('#location').text(detail.user_name);
					$('#updated_at').text(detail.updated_at);
					$('#created_at').text(detail.created_at);
				}else if(result.code == 1){
					$.toast("文件不存在", "cancel");
					setTimeout(function() {
						self.location=document.referrer;
					}, 1000)
				}else if(result.code == -1){
					location.href = '/error';
				}
			}
		});
	}
	//收藏文件
	function collectFile(){
		$.ajax({
			type: 'get',
			url: ' /collect_file/collect/'+file_id,
			dataType: 'json',
			success: function (result) {
				if (result.code == 0) {
					$('.fOpt .fav').toggleClass("ck");
				}else if(result.code == -1){
					location.href = '/error';
				}
			}
		});
	}
	//取消收藏文件
	function cancelCollectFile(){
		$.ajax({
			type: 'get',
			url: ' /collect_file/cancel/'+file_id,
			dataType: 'json',
			success: function (result) {
				if (result.code == 0) {
					$('.fOpt .fav').toggleClass("ck");
				}else if(result.code == -1){
					location.href = '/error';
				}
			}
		});
	}
	//删除文件或文件夹
	function deleteFile(){
		$.ajax({
			type: 'get',
			url: ' /user_file/delete/'+file_id,
			dataType: 'json',
			success: function (result) {
				if (result.code == 0) {
					location.href = '/list?folder_id=' + folder_id+'&is_public='+is_public;
				}else if(result.code == -1){
					location.href = '/error';
				}
			}
		});
	}
	//下载文件
	function download(){
		$.ajax({
			type: 'get',
			url: '/download/auth/'+file_id,
			data:{is_public:is_public},
			dataType: 'json',
			success: function (result) {
				if (result.code == 0) {
					var is_image = $('.download').attr('is_image');
					if(is_image == '0'){
						var u = navigator.userAgent, app = navigator.appVersion;
						var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
						if(isIOS){
							download_links = "{{ Illuminate\Support\Facades\Request::server ('SERVER_NAME') }}" +'/download/' + result.data.access_token;
							$('#show-custom').trigger('click');
						}else{
							location.href = '/download/' + result.data.access_token;
						}
					}else{
						location.href = '/download/' + result.data.access_token;
					}
				}else if(result.code == 1){
					$.toast("文件不存在", "cancel");
					setTimeout(function() {
						location.href = '/list?folder_id=' + folder_id+'&is_public='+is_public;
					}, 1000)
				}else if(result.code == -1){
					location.href = '/error';
				}
			}
		});
	}
	//判断文件类型
	function getIconClass(file_ext){
		switch (file_ext){
			case '.DOC':
			case '.DOCX':
				icon = 'doc';
				break;
			case '.TXT':
				icon = 'txt';
				break;
			case '.ZIP':
			case '.RAR':
				icon = 'zip';
				break;
			case '.PPT':
				icon = 'ppt';
				break;
			case '.XLS':
			case '.XLSX':
				icon = 'excel';
				break;
			default :
				icon = 'common';
		}
		return icon;
	}
//回复相关
/*function showPubTop(){
	$(".pubBoxTop").show();
	$(".mask").show();
}
function hidePubTop(){
	$(".pubBoxTop").hide();
	$(".mask").hide();
	$(".pubBox").show()
}

$('.editorCont').on("click",function(){
	$(".pubBox").hide();
	showPubTop();
	$('.editorContTop').focus();
});
$(".pubBtnCancel,.mask").click(function(){
	hidePubTop()
});*/
//回复相关end
</script>
@stop